<?php


namespace App\Http\Controllers\Api\Finance;


use App\Enum\WithdrawEnum;
use App\Exports\CarDriverExport;
use App\Exports\WithdrawExport;
use App\Http\Controllers\Controller;
use App\Http\Requests\StateRequest;
use App\Models\CompanyManagement;
use App\Models\DriverRecordLog;
use App\Models\DriverWallet;
use App\Repositories\CarRepository;
use App\Repositories\DriverRecordLogRepository;
use App\Repositories\WithdrawRepository;
use App\Transforms\WithdrawTransformer;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class WithdrawController extends Controller
{


    /**
     * 提现分页列表
     * @param Request $request
     * @param WithdrawRepository $repository
     * @return WithdrawController|JsonResponse
     * @Author 姿势就是力量
     */
    public function getPageList(Request $request, WithdrawRepository $repository)
    {
        $companyId = (int)$request->get('company_id', '2');                //公司id
        $rules = (string)$request->get('rules', '0');                //0 网约车  1 代驾
        $date =(string)$request->get('created_at','');      //时间选择
        $status = (string)$request->get('status', '');                  //状态
        $withdraw_sn = (string)$request->get('withdraw_sn', '');       //提现流水号
        $money = (string)$request->get('money', '');                  //提现金额
        $real_name = (string)$request->get('real_name', '');         //司机姓名
        $phone= (string)$request->get('phone', '');                         //司机手机号
        $remit_sn= (string)$request->get('remit_sn', '');                   //第三方流水号
        $account= (string)$request->get('account', '');                    //提现账号
        $bank_name= (string)$request->get('bank_name', '');               //银行名称
        $account_name= (string)$request->get('account_name', '');        //开户姓名
        $type= (string)$request->get('type', '');                          //提现渠道
        $review_time =(string)$request->get('review_time','');      //审核时间
        $pageSize = (int)$request->get('pageSize', 10);                //分页
        $list = $repository->getPageList($companyId,$date, $status, $withdraw_sn,$money,$real_name,$phone,$remit_sn,$account,$bank_name,$account_name, $review_time,$type,$rules,$pageSize);
        $list['data'] = (new WithdrawTransformer())->transformList($list['data']);
        $list['count'] = $repository->sumMoney($companyId, $date, $status);

        return $this->sendJson($list);
    }


    /**
     * 保存信息
     * @param Request $request
     * @param WithdrawRepository $repository
     * @return WithdrawController|JsonResponse
     * @Author 姿势就是力量
     */
    public function save(Request $request, WithdrawRepository $repository)
    {
        $data = $request->post();
        $key = 'withdraw_' . $data['id'];
        Cache::forget($key);
        if (Cache::has($key)) {
            return $this->sendError('系统繁忙！');
        }
        Cache::forever($key, $data['id']);
        if ($data['status'] == WithdrawEnum::REJECT) {
            Cache::forget($key);
            return $this->sendError('请勿重复提交！');
        }
        $withdraw = $repository->saveData($data);
        if ($withdraw->status == WithdrawEnum::REJECT) {
            //开启事务
            DB::transaction(function ($withdraw) {
                //退款到司机余额
                $wallets = DriverWallet::where('driver_id', $withdraw->driver_id)->where('type',$withdraw->type)->first();
                $wallets->amount += $withdraw->money;
                $wallets->balance += $withdraw->money;
                $wallets->save();
                //添加流水记录
                DriverRecordLog::query()->create([
                    'driver_id' => $withdraw->driver_id,
                    'name' => '提现失败',
                    'money' => $withdraw->money,
                    'stard_time' => $withdraw->created_at,
                    'end_time' => now(),
                    'order_sn' => $withdraw->withdraw_sn,
                    'status' => 3,
                    'note' => '提现失败',
                    'type' => 1,
                    'set_type' => 1,
                    'in_type' => 1,
                    'rules' => $withdraw->type
                ]);
            });
        }
        Cache::forget($key);
        if (!$withdraw) return $this->sendError('保存失败');

        return $this->sendSuccess('保存成功');
    }

    /**
     * 导出订单
     * @param StateRequest $request
     * @param CarRepository $repository
     * @Author 姿势就是力量
     */


    public function WithdrawExcel(Request $request,WithdrawRepository $repository)
    {
        $user_id = $request->get('management_id','');  //查所属公司
        $company = (new CompanyManagement())->where('management_id',$user_id)->get()->toArray();  //查所属公
        $companyIds =array_column($company, 'company_id');   //查所属公司
        $companyId = (int)$request->get('company_id', '');                //公司id
        $date =(string)$request->get('created_at','');      //时间选择
        $status = (string)$request->get('status', '');                  //状态
        $withdraw_sn = (string)$request->get('withdraw_sn', '');       //提现流水
        $money = (string)$request->get('money', '');                  //提现金额
        $real_name = (string)$request->get('real_name', '');         //司机姓名
        $phone= (string)$request->get('phone', '');                         //司机手机号
        $remit_sn= (string)$request->get('remit_sn', '');                   //第三方流水号
        $account= (string)$request->get('account', '');                    //提现账号
        $bank_name= (string)$request->get('bank_name', '');               //银行名称
        $account_name= (string)$request->get('account_name', '');        //开户姓名
        $type= (string)$request->get('type', '');                          //提现渠道
        $review_time =(string)$request->get('review_time','');      //审核时间
        $list = $repository->Excel_getPageList($companyId,$companyIds,$date, $status, $withdraw_sn,$money,$real_name,$phone,$remit_sn,$account,$bank_name,$account_name, $review_time,$type);
        $list['data'] = (new WithdrawTransformer())->transformList($list);
        $list['count'] = $repository->sumMoney($companyId, $date, $status);
        $withdraw = $list['data'];
        $row = [
            [
                "id" => 'ID',
                "withdraw_sn" => '提现流水号',
                "created_at" => '申请提现时间',
                "status" => '提现状态',
                "money" => '提现金额',
                "real_name" => '司机姓名',
                "phone" => '司机手机号',
                "name" => '审核人',
                "review_time" => '审核时间',
                "type" => '提现渠道',
                "account" => '提现账户',
                "bank_name" => '银行名称',
                "account_name" => '开户姓名',
                "remit_sn" => '第三方流水号',
            ]
        ];

        return Excel::download(new  WithdrawExport($row,$withdraw), '提现.xls');
    }


}
